<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/14
 * Time: 16:54
 */
namespace common\logics\auditclassify;

use Yii;
use common\widgets\Tool;
use yii\base\BaseObject;

class AuditClassifyLogic extends BaseObject implements IAuditClassify
{
    /** @var \common\sources\read\auditclassify\AuditClassifyAccess $RAuditClassifyAccess **/
    private $RAuditClassifyAccess;
    /** @var \common\sources\write\auditclassify\AuditClassifyAccess $WAuditClassifyAccess **/
    private $WAuditClassifyAccess;

    public function init()
    {
        $this->RAuditClassifyAccess = Yii::$container->get("RAuditClassifyAccess");
        $this->WAuditClassifyAccess = Yii::$container->get("WAuditClassifyAccess");
        parent::init(); // TODO: Change the autogenerated stub
    }
    //获取分类列表
    public function getList($param)
    {
	    $where = ' 1=1 ';
	    if($param['audit_type']){
		    $where = ' pid = '.$param['audit_type'];
	    }

        $list = $this->RAuditClassifyAccess->newGetList($where);
        return Tool::returnJson($list,200,"成功");
    }
    //添加分类
    public function addClassify($data)
    {
        if(empty($data['name'])){
            return Tool::returnJson([],401,"分类名称不能为空");
        }
	    if(empty($data['pid'])){
		    return Tool::returnJson([],401,"分类类型不能为空");
	    }
        //查看分类名称是否存在
        $classify = $this->RAuditClassifyAccess->getClassify($data['name']);
        if(!empty($classify)){
            return Tool::returnJson([],402,"该分类已经存在,请重新输入");
        }
        //添加分类
        $result = $this->WAuditClassifyAccess->addClassify($data['name'],$data['pid']);
        if($result){
            return Tool::returnJson([],200,"成功");
        }
        return Tool::returnJson([],403,"添加分类失败");
    }
    //修改分类
    public function modifyClassify($data)
    {
        if(empty($data['a_id'])){
            return Tool::returnJson([],401,"分类id不能为空");
        }
        if(empty($data['name'])){
            return Tool::returnJson([],402,"分类名称不能为空");
        }
	    if(empty($data['pid'])){
		    return Tool::returnJson([],402,"分类类型不能为空");
	    }
        //查看分类名称是否存在
        $classify = $this->RAuditClassifyAccess->newGetClassify($data['name'],$data['a_id']);
        if(!empty($classify)){
            return Tool::returnJson([],403,"该分类已经存在,请重新输入");
        }
        $result = $this->WAuditClassifyAccess->modifyClassify($data);
        if($result){
            return Tool::returnJson([],200,"修改成功");
        }
        return Tool::returnJson([],405,"修改失败");
    }
    //删除分类
    public function delClassify($data)
    {
        if(empty($data['a_id'])){
            return Tool::returnJson([],401,"分类id不能为空");
        }
        $result = $this->WAuditClassifyAccess->delClassify($data);
        if($result){
            return Tool::returnJson([],200,"删除成功");
        }
        return Tool::returnJson([],402,"删除失败");
    }

}